package com.kun.valiant.web.controller.valiant;

import com.kun.valiant.common.annotation.Log;
import com.kun.valiant.common.core.controller.BaseController;
import com.kun.valiant.common.core.domain.AjaxResult;
import com.kun.valiant.common.core.page.TableDataInfo;
import com.kun.valiant.common.enums.BusinessType;
import com.kun.valiant.common.utils.poi.ExcelUtil;
import com.kun.valiant.common.validator.CreateGroup;
import com.kun.valiant.course.domain.Course;
import com.kun.valiant.course.domain.vo.CourseVO;
import com.kun.valiant.course.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 课程管理 前端控制器
 * @author fangkun
 * @since 2023-06-06
 */
@RestController
@RequestMapping("/valiant/course")
public class CourseController extends BaseController {

    @Autowired
    ICourseService courseService;

    /**
     * 课程列表
     * @return
     */
    @PreAuthorize("hasPermission('valiant:course:list')")
    @GetMapping("/list")
    public TableDataInfo getCourseList(CourseVO courseVO) {
        List<Course> list = courseService.getCourseList(courseVO);
        return getDataTable(list);
    }

    /**
     * 增加课程
     * @param courseVO
     * @return
     */
    @PreAuthorize("hasPermission('valiant:course:create')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addCourse(@Validated(CreateGroup.class) @RequestBody CourseVO courseVO) {
        return courseService.addCourse(courseVO);
    }

    /**
     * 根据id查询课程
     * @param courseId
     * @return
     */
    @PreAuthorize("hasPermission('valiant:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @GetMapping("/{courseId}")
    public AjaxResult getCourseById(@PathVariable Integer courseId) {
        return AjaxResult.success(courseService.getCourseById(courseId));
    }

    /**
     * 修改课程信息
     * @param courseVO
     * @return
     */
    @PreAuthorize("hasPermission('valiant:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult editCourse(@Validated @RequestBody CourseVO courseVO) {
        return courseService.editCourse(courseVO);
    }

    /**
     * 根据ID删除
     * @param courseIds
     * @return
     */
    @PreAuthorize("hasPermission('valiant:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{courseIds}")
    public AjaxResult removeChannel(@PathVariable Integer[] courseIds) {
        return toAjax(courseService.removeCourseIds(courseIds));
    }

    /**
     * 导出Excel
     * @param response
     * @param courseVO
     */
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PreAuthorize("hasPermission('valiant:course:export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, CourseVO courseVO) {
        List<Course> list = courseService.getCourseList(courseVO);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程数据");
    }
}
